﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace SuperManagement.LoadPage
{
    /// <summary>
    /// OrderManagement.xaml 的交互逻辑
    /// </summary>
    public partial class OrderManagement : Page
    {
        private DispatcherTimer ShowTimer;
        public OrderManagement()
        {
            InitializeComponent();
            ShowTimer = new System.Windows.Threading.DispatcherTimer();

            ShowTimer.Tick += new EventHandler(ShowCurTimer);//起个Timer一直获取当前时间 

            ShowTimer.Interval = new TimeSpan(0, 0, 0, 1, 0);
            ShowTimer.Start();
        }
        string time = "";
        public void ShowCurTimer(object sender, EventArgs e)

        {

            //获得年月日 

            time += DateTime.Now.ToString("yyyy年MM月dd日");   //yyyy年MM月dd日 

        }
        private void Search_Click(object sender, RoutedEventArgs e)
        {
            using (var context = new HBEntities())
            {
                var q1 = from t1 in context.Order
                         from t2 in context.Order_List
                         where t1.OId == t2.OId
                         select new
                         {
                             日期 = t1.Time,
                             金额 = t1.Sum,
                             商品ID = t2.PId,
                             数量 = t2.Amount
                         };
                searchData.ItemsSource = q1.ToList();
            }
        }
        private void ShowResult()
        {
            using (var context = new HBEntities())
            {
                var q = from t in context.Order
                        select new
                        {
                            订单号 = t.OId,
                            员工ID = t.SId,
                            状态 = t.State
                        };
                searchData.ItemsSource = q.ToList();
            }
        }
        private void ShowResult2()
        {
            using (var context = new HBEntities())
            {
                var q = from t in context.Order_List
                        select new
                        {
                            订单号 = t.OId,
                            商品ID = t.PId,
                            数量 = t.Amount
                        };
                searchData.ItemsSource = q.ToList();
            }
        }
        private void CreateCheck_Click(object sender, RoutedEventArgs e)
        {
            string s1 = this.CheckNum.Text.ToString();
            int s2 = int.Parse(s1);
            string s3 = UserID.Text.ToString();
            int s4 = int.Parse(s3);
            if (s3 == null || s1 == null)
            {
                MessageBox.Show("please enter your ID and select OrderID!");
            }
            else
            {
                using (var context = new HBEntities())
                {
                    Order order = new Order()
                    {
                        OId = s2,
                        State = "Avaliable",
                        SId = s4
                    };

                    try
                    {
                        context.Order.Add(order);
                        context.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("添加失败：" + ex.Message);
                    }
                }
                ShowResult();
            }


        }

        private void ConfirmOrder_Click(object sender, RoutedEventArgs e)
        {
            string a1 = PID.Text.ToString();
            int a5 = int.Parse(a1);
            string a2 = PName.Text.ToString();
            string a3 = Amount.Text.ToString();
            float a4 = float.Parse(a3);
            if (a1 == null || a2 == null || a3 == null)
            {
                MessageBox.Show("please enter productID, productName,product Amount!");
            }
            else
            {
                using (var context = new HBEntities())
                {
                    Order_List order1 = new Order_List()
                    {
                        PId = a5,
                        Amount = a4,
                    };

                    try
                    {
                        context.Order_List.Add(order1);
                        context.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("添加失败：" + ex.Message);
                    }
                }
                ShowResult2();
            }
        }
    }
}

